<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { loginApi } from '@/api/login'

const loginForm = ref({
  username: '',
  password: ''
})

const loading = ref(false)
const router = useRouter()

// 登录
const login = async () => {
  // 简单验证
  if (!loginForm.value.username || !loginForm.value.password) {
    ElMessage.warning('请输入用户名和密码')
    return
  }

  loading.value = true
  try {
    const result = await loginApi(loginForm.value)
    console.log('登录结果:', result)
    // 修改这里，正确处理响应数据
    if (result.data.code == 200) {
      ElMessage.success('登录成功')
      // 根据新的返回格式处理数据
      const loginUser = {
        name: loginForm.value.username,
        token: result.data.data
      }
      localStorage.setItem('loginUser', JSON.stringify(loginUser))
      
      router.push('/')
    } else {
      ElMessage.error(result.msg || '登录失败')
    }
// ... existing code ...
  } catch (error) {
    console.error('登录异常:', error)
    // 更详细地显示错误信息
    if (error.message) {
      ElMessage.error('登录请求失败: ' + error.message)
    } else {
      ElMessage.error('登录请求失败，请检查网络连接或服务器状态')
    }
  } finally {
    loading.value = false
  }
}

//取消
const cancel = () => {
  loginForm.value = {
    username: '',
    password: ''
  }
}
</script>

<template>
  <div id="container">
    <div class="login-form">
      <el-form 
        label-width="80px" 
        :model="loginForm" 
        @keyup.enter="login"
      >
        <p class="title">仲恺农业工程学院</p>
        <el-form-item label="用户名" prop="username">
          <el-input 
            v-model="loginForm.username" 
            placeholder="请输入用户名"
            clearable
          ></el-input>
        </el-form-item>
        
        <el-form-item label="密码" prop="password">
          <el-input 
            type="password" 
            v-model="loginForm.password" 
            placeholder="请输入密码"
            show-password
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button 
            class="button" 
            type="primary" 
            @click="login" 
            :loading="loading"
          >
            {{ loading ? '登录中...' : '登 录' }}
          </el-button>
          <el-button class="button" type="info" @click="cancel">重 置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style scoped>
#container {
  padding: 10%;
  height: 410px;
  background-image: url('../../assets/bg1.jpg');
  background-repeat: no-repeat;
  background-size: cover;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-form {
  max-width: 400px;
  padding: 30px;
  margin: 0 auto;
  border: 1px solid #e0e0e0;
  border-radius: 10px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
  background-color: white;
}

.title {
  font-size: 30px;
  font-family: '楷体';
  text-align: center;
  margin-bottom: 30px;
  font-weight: bold;
}

.button {
  margin-top: 10px;
  width: 120px;
}
</style>